﻿//Copyright Notice:  ©2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Text;
using System.Security.Principal;
using System.DirectoryServices;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using Microsoft.InformationSecurity.CISF.Security.AuthZServices;

namespace Microsoft.InformationSecurity.CISF.Security.Principal
{
    /// <summary>
    /// A CISF Identity object based on an existing Identity.
    /// </summary>
    public class CISFIdentity : IIdentity
    {
        #region Private Variables
        private string name = "";
        private IdentityName identityName;
        private string authenticationType = "";
        private bool isAuthenticated;

        #endregion

        #region Constructor
        /// <summary>
        /// A CISF Identity object based on an existing Identity.
        /// </summary>
        /// <param name="identity">Identity object.</param>
        public CISFIdentity(IIdentity identity, User user)
        {
            LoadIdentityData(identity, user);
        }

        #endregion

        #region Methods

        #region LoadIdentityData
        /// <summary>
        /// Loads the Identity and user information into the CISFIdentity object.
        /// </summary>
        /// <param name="identity">An IIdentity object.</param>
        /// <param name="user">a User object.</param>
        private void LoadIdentityData(IIdentity identity, User user)
        {
            if (identity == null)
                throw new ArgumentException("Invalid Identity provided.  Unable to load the CISFIdentity object.");

            // We are not using this for now. 
            //this.authenticationType = identity.AuthenticationType;
            this.isAuthenticated = identity.IsAuthenticated;
            this.name = identity.Name;

            identityName = new IdentityName(identity.Name);

            if (user != null)
            {
                identityName.PersonnelNumber = user.PersonnelNumber;
                identityName.DisplayName = user.DisplayName;
                identityName.PersonId = user.PersonId;
                identityName.FullName = user.FullName;
                identityName.EMailName = user.EMailName;
            }
        }

        #endregion

        #endregion

        #region Properties

        #region IIdentity Members

        #region AuthenticationType
        /// <summary>
        /// Gets the type of authentication used to identify the user.
        /// </summary>
        public string AuthenticationType
        {
            get { return this.authenticationType; }
        }

        #endregion

        #region IsAuthenticated
        /// <summary>
        /// Gets a value indicating whether the user has been authenticated.
        /// </summary>
        public bool IsAuthenticated
        {
            get { return this.isAuthenticated; }
        }

        #endregion

        #region Name
        /// <summary>
        /// Get's the user's logon name without the domain prefix.
        /// </summary>
        public string Name
        {
            get { return this.identityName.Name; }
        }

        #endregion

        #endregion

        #region PersonId
        /// <summary>
        /// Gets the user's PeriodId from database record.
        /// </summary>
        public int PersonId
        {
            get { return this.identityName.PersonId; }
        }

        #endregion

        #region Alias
        /// <summary>
        /// Gets the user's Alias.
        /// </summary>
        public string Alias
        {
            get { return this.identityName.Alias; }
        }

        #endregion

        #region Domain
        /// <summary>
        /// Gets the user's domain.
        /// </summary>
        public string Domain
        {
            get { return this.identityName.Domain; }
        }

        #endregion

        #region EMailName
        /// <summary>
        /// Gets the user's EMail Name.
        /// </summary>
        public string EMailName
        {
            get { return this.identityName.EMailName; }
        }

        #endregion

        #region FullName
        /// <summary>
        /// Gets the user's Full Name.
        /// </summary>
        public string FullName
        {
            get { return this.identityName.FullName; }
        }

        #endregion

        #region DisplayName
        /// <summary>
        /// Gets the user's Display Name.
        /// </summary>
        public string DisplayName
        {
            get { return this.identityName.DisplayName; }
        }

        #endregion

        #region PersonnelNumber
        /// <summary>
        /// Gets or sets the PersonnelNumber.
        /// </summary>
        public int PersonnelNumber
        {
            get { return this.identityName.PersonnelNumber; }
            set { this.identityName.PersonnelNumber = value; }
        }

        #endregion

        #region IdentityName
        /// <summary>
        /// Gets the user's IdentityName object.
        /// </summary>
        public IdentityName IdentityName
        {
            get { return this.identityName; }
        }

        #endregion

        #endregion

    }
}
